﻿using EntityPractica.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EntityPractica.Controllers
{
    public class HomeController : Controller
    {
        private AlumnoContenedor alumnosContenedor = new AlumnoContenedor();

        //
        // GET: /Home/  

        public ActionResult Index()
        {
            var laListaDeAlumnos = alumnosContenedor.Alumnos;
            // laListaDeLinks.Count();
            return View(model: laListaDeAlumnos);
        }

        [HttpGet]
        public ActionResult Agregar()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Agregar(Alumno unAlumno)
        {
            alumnosContenedor.agregarAlumno(unAlumno);
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult Editar(int Dni)
        {
            Alumno unAlumno = alumnosContenedor.Alumnos.Find(Dni);
            return View(model: unAlumno);
        }

        [HttpPost]
        public ActionResult Editar(Alumno unAlumno)
        {
            Alumno alumnoBase = alumnosContenedor.Alumnos.Find(unAlumno.Dni);
            alumnoBase.Nombre= unAlumno.Nombre;
            alumnoBase.Apellido = unAlumno.Apellido;

            alumnosContenedor.SaveChanges();

            return RedirectToAction("Index");
        }
        
      /*  [HttpGet]
        public ActionResult Borrar()
        {
            return View();
        }

        
        
        [HttpPost]
        public ActionResult Borrar(Alumno unAlumno)
        {
            alumnosContenedor.borrarAlumno(unAlumno);
            return RedirectToAction("Index");
        }
        */
        public ActionResult Borrar(int Dni) 
        {
            var dataContext = new AlumnoContenedor();
            var alumno = dataContext.Alumnos.Find(Dni);
            dataContext.Alumnos.Remove(alumno);
            dataContext.SaveChanges();
            return RedirectToAction("Index");
        }

    }
}